/*
 * readWavFile.cpp
 *
 *  Created on: Sep 23, 2012
 *      Author: shaohong
 */



#include <iostream>
#include <string>
#include <fstream>

using namespace std;


/* Class WAVE Header info */
class readWavHeader
{
public:
    struct wavHeaderInfo
    {
        char                RIFF[4];        /* RIFF Header      */ //Magic header
        unsigned long       ChunkSize;      /* RIFF Chunk Size  */
        char                WAVE[4];        /* WAVE Header      */
        char                fmt[4];         /* FMT header       */
        unsigned long       Subchunk1Size;  /* Size of the fmt chunk                                */
        unsigned short      AudioFormat;    /* Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM */
        unsigned short      NumOfChan;      /* Number of channels 1=Mono 2=Sterio                   */
        unsigned long       SamplesPerSec;  /* Sampling Frequency in Hz                             */
        unsigned long       bytesPerSec;    /* bytes per second */
        unsigned short      blockAlign;     /* 2=16-bit mono, 4=16-bit stereo */
        unsigned short      bitsPerSample;  /* Number of bits per sample      */
        char                Subchunk2ID[4]; /* "data"  string   */
        unsigned long       Subchunk2Size;  /* Sampled data length    */
    } wavHeader;

    readWavHeader::wavHeaderInfo readWavHeaderFromFile(string filenameWithPath);
    readWavHeader();
    ~readWavHeader();
};

readWavHeader::wavHeaderInfo readWavHeaderFromFile(string filenameWithPath)
{
    readWavHeader::wavHeaderInfo waveHeaderData;
    fstream binary_file(filenameWithPath.c_str(),ios::binary|ios::in);
    binary_file.read(reinterpret_cast<char *>(&waveHeaderData),sizeof(readWavHeader::wavHeaderInfo));
    binary_file.close();
    return waveHeaderData;
}

readWavHeader::readWavHeader()
{
    cout << "";
}

readWavHeader::~readWavHeader()
{
    cout << "";
}



int main(int argc, char ** argv)
{
    readWavHeader objReadWavHeader;
    readWavHeader::wavHeaderInfo wHeader;
    string fileName = string(argv[1]);
    cout << "file: " << fileName << endl;
    wHeader = readWavHeaderFromFile(fileName);
    cout << "WAVE HEADER INFO\n";
    cout << "------------------------------------\n";
    cout << "RIFF chunk Found   :" << wHeader.RIFF << "\n";
    cout << "Chunk Size         :" << wHeader.ChunkSize << "\n";
    cout << "Wave chunk Found   :" << wHeader.WAVE << "\n";
    cout << "Fmt chunk Found    :" << wHeader.fmt << "\n";
    cout << "Audio Format       :" << wHeader.AudioFormat << "\n";
    cout << "Number of Channels :" << wHeader.NumOfChan << "\n";
    cout << "Samples Per Sec    :" << wHeader.SamplesPerSec << "\n";
    cout << "Bits per Sample    :" << wHeader.bitsPerSample << "\n";
    return 0;
}
